package com.bamboo.toolbox.hotdeploy.dialog;

import com.bamboo.toolbox.core.swing.OkDialog;
import com.bamboo.toolbox.core.utils.Base64Util;
import com.bamboo.toolbox.core.utils.FileUtils;
import com.bamboo.toolbox.core.utils.HttpUtils;
import com.bamboo.toolbox.core.utils.JsonUtils;
import com.bamboo.toolbox.hotdeploy.EventType;
import com.bamboo.toolbox.hotdeploy.model.DeployCmd;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;

/**
 * @author: luzhiqing
 * @date: 2022-10-19
 **/
@Slf4j
public class DeployDialog extends DialogWrapper {

    private final JPanel centerPanel;

    private final JLabel labelUrl;
    private final JTextField url;
    private final JLabel fileLabel;
    private final JTextField file;
    private final JLabel eventLabel;
    private final JComboBox<EventType> eventType;

    private final SpringLayout layout;

    private final Project project;


    public DeployDialog(@Nullable Project project) {
        super(project);
        layout = new SpringLayout();

        centerPanel = new JPanel(layout);
        labelUrl = new JLabel("目标服务：");
        url = new JTextField();
        centerPanel.add(labelUrl);
        centerPanel.add(url);

        fileLabel = new JLabel("部署文件：");
        file = new JTextField();
        file.setEditable(false);
        file.setHorizontalAlignment(JTextField.RIGHT);
        centerPanel.add(fileLabel);
        centerPanel.add(file);

        eventLabel = new JLabel("部署方式：");
        eventType = new ComboBox<>(EventType.values());
        eventType.setSelectedIndex(0);
        centerPanel.add(eventLabel);
        centerPanel.add(eventType);

        this.project = project;
    }

    protected void constraintsLayout() {


        SpringLayout.Constraints lableUrlCons = layout.getConstraints(labelUrl);
        lableUrlCons.setHeight(Spring.constant(45));
        lableUrlCons.setWidth(Spring.constant(70));
        lableUrlCons.setX(Spring.constant(5));
        lableUrlCons.setY(Spring.constant(5));

        SpringLayout.Constraints urlCons = layout.getConstraints(url);
        urlCons.setHeight(Spring.constant(45));
        urlCons.setWidth(Spring.constant(600));
        urlCons.setX(Spring.sum(lableUrlCons.getConstraint(SpringLayout.EAST), Spring.constant(5)));
        urlCons.setY(lableUrlCons.getY());

        SpringLayout.Constraints fileLableCons = layout.getConstraints(fileLabel);
        fileLableCons.setHeight(Spring.constant(45));
        fileLableCons.setWidth(Spring.constant(70));
        fileLableCons.setX(lableUrlCons.getX());
        fileLableCons.setY(Spring.sum(lableUrlCons.getConstraint(SpringLayout.SOUTH), Spring.constant(5)));

        SpringLayout.Constraints fileCons = layout.getConstraints(file);
        fileCons.setHeight(Spring.constant(45));
        fileCons.setWidth(Spring.constant(600));
        fileCons.setX(Spring.sum(lableUrlCons.getConstraint(SpringLayout.EAST), Spring.constant(5)));
        fileCons.setY(Spring.sum(lableUrlCons.getConstraint(SpringLayout.SOUTH), Spring.constant(5)));

        SpringLayout.Constraints eventLableCons = layout.getConstraints(eventLabel);
        eventLableCons.setHeight(Spring.constant(45));
        eventLableCons.setWidth(Spring.constant(70));
        eventLableCons.setX(fileLableCons.getX());
        eventLableCons.setY(Spring.sum(fileLableCons.getConstraint(SpringLayout.SOUTH), Spring.constant(5)));

        SpringLayout.Constraints eventCons = layout.getConstraints(eventType);
        eventCons.setHeight(Spring.constant(45));
        eventCons.setWidth(Spring.constant(600));
        eventCons.setX(Spring.sum(fileLableCons.getConstraint(SpringLayout.EAST), Spring.constant(5)));
        eventCons.setY(Spring.sum(fileLableCons.getConstraint(SpringLayout.SOUTH), Spring.constant(5)));


        SpringLayout.Constraints centerPanelCons = layout.getConstraints(centerPanel);
        centerPanelCons.setWidth(Spring.constant(700));
        centerPanelCons.setHeight(Spring.constant(150));

    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        return centerPanel;
    }


    @Override
    public void init() {
        super.init();
        setOKButtonText("Deploy");

        url.setName("URL");
        url.setText("http://localhost:8084/hotswap/deploy");

        file.setName("FILE");
        this.constraintsLayout();
    }

    @Override
    protected void doOKAction() {
        boolean success = false;
        try {
            success = doOk();
        } finally {
            super.doOKAction();
            OkDialog okDialog = new OkDialog(project, success ? "部署成功" : "部署失败");
            okDialog.setModal(true);
            okDialog.show();
        }
    }

    protected boolean doOk() {
        byte[] bytes = FileUtils.read(file.getText());
        String encode = Base64Util.encode(bytes);
        DeployCmd deployCmd = new DeployCmd();
        deployCmd.setClassDefinition(encode);
        int selectedIndex = eventType.getSelectedIndex();
        deployCmd.setEventType(EventType.values()[selectedIndex].name());
        deployCmd.setPath(file.getText());
        log.info("发送热部署事件{}", url + ":" + JsonUtils.toJson(deployCmd));
        String post = HttpUtils.post(url.getText(), deployCmd);
        // 先这样
        return "\"0\"".equals(post);
    }

    public JPanel getCenterPanel() {
        return centerPanel;
    }

    public JTextField getUrl() {
        return url;
    }

    public JTextField getFile() {
        return file;
    }
}
